﻿/// <reference path="jquery-1.5.1-vsdoc.js" />

/* Mvc Framework Functions For Telerik or Custom Status ...  */

// 0.1- Telerik Extensions/AddOn...

var $t = $.telerik;

var telerik_textbox_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_textbox_Loaded = (typeof ($t.textbox) != 'undefined');

if (telerik_textbox_Loaded) {
  // Numeric TextBox Focus...
  $t.textbox.prototype.focus = function () {
    if (this.enabled) {
      this._showTextBoxValue();
      this.$text.hide();
      var input = this.$element[0];
      setTimeout(function () {
        input.focus();
        if ($.browser.msie) {
          input.select();
        }
        else {
          input.selectionStart = 0;
          input.selectionEnd = input.value.length;
        }
      }, 0);
    }
  }

  // Numeric TextBox Show...
  $t.textbox.prototype.show = function () {
    var input = this.$element[0];
    $(input).parent().show();
  }

  // Numeric TextBox Hide...
  $t.textbox.prototype.hide = function () {
    var input = this.$element[0];
    $(input).parent().hide();
  }
}

var telerik_combobox_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_combobox_Loaded = (typeof ($t.combobox) != 'undefined');

if (telerik_combobox_Loaded) {
  // ComboBox Show...
  $t.combobox.prototype.show = function () {
    var wrapper = this.$wrapper;
    wrapper.show();
  }

  // ComboBox Hide...
  $t.combobox.prototype.hide = function () {
    var wrapper = this.$wrapper;
    wrapper.hide();
  }

  // ComboBox Focus...
  $t.combobox.prototype.focus = function () {
    if (this.enabled) {
      var input = this.$text;

      setTimeout(function () {
        input.focus();
      },
    0);
    }
  }
}

var telerik_datepicker_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_datepicker_Loaded = (typeof ($t.datepicker) != 'undefined');


if (telerik_datepicker_Loaded) {
  // DatePicker Show...
  $t.datepicker.prototype.show = function () {
    var wrapper = this.$wrapper;
    wrapper.show();
  }

  // DatePicker Hide...
  $t.datepicker.prototype.hide = function () {
    var wrapper = this.$wrapper;
    wrapper.hide();
  }

  // DatePicker Focus...
  $t.datepicker.prototype.focus = function () {
    if (this.enabled) {
      var input = this.$element;

      setTimeout(function () {
        var openSelectBox = this.openOnFocus;
        this.openOnFocus = false;

        input.focus();
        if ($.browser.msie) {
          input.select();
        }
        else {
          input.selectionStart = 0;
          input.selectionEnd = input.value.length;
        }

        this.openOnFocus = openSelectBox;
      }, 0);
    }
  }
}

var telerik_timepicker_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_timepicker_Loaded = (typeof ($t.timepicker) != 'undefined');


if (telerik_timepicker_Loaded) {

  // TimePicker Show...
  $t.timepicker.prototype.show = function () {
    var wrapper = this.$wrapper;
    wrapper.show();
  }

  // TimePicker Hide...
  $t.timepicker.prototype.hide = function () {
    var wrapper = this.$wrapper;
    wrapper.hide();
  }

  // TimePicker Focus...
  $t.timepicker.prototype.focus = function () {
    if (this.enabled) {
      var input = this.$element;

      setTimeout(function () {
        var openSelectBox = this.openOnFocus;
        this.openOnFocus = false;

        input.focus();
        if ($.browser.msie) {
          input.select();
        }
        else {
          input.selectionStart = 0;
          input.selectionEnd = input.value.length;
        }

        this.openOnFocus = openSelectBox;
      }, 0);
    }
  }
}

var telerik_datetimepicker_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_datetimepicker_Loaded = (typeof ($t.datetimepicker) != 'undefined');


if (telerik_datetimepicker_Loaded) {

  // DateTimePicker Show...
  $t.datetimepicker.prototype.show = function () {
    var wrapper = this.$wrapper;
    wrapper.show();
  }

  // DateTimePicker Hide...
  $t.datetimepicker.prototype.hide = function () {
    var wrapper = this.$wrapper;
    wrapper.hide();
  }

  // DateTimePicker Focus...
  $t.datetimepicker.prototype.focus = function () {
    if (this.enabled) {
      var input = this.$element;

      setTimeout(function () {
        input.focus();
        if ($.browser.msie) {
          input.select();
        }
        else {
          input.selectionStart = 0;
          input.selectionEnd = input.value.length;
        }
      }, 0);
    }
  }
}

var telerik_autocompleter_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_autocompleter_Loaded = (typeof ($t.autocomplete) != 'undefined');


if (telerik_autocompleter_Loaded) {

  // ComboBox AutoComplete show...
  $t.autocomplete.prototype.show = function () {
    this.$text.show();
  }

  // ComboBox AutoComplete hide...
  $t.autocomplete.prototype.hide = function () {
    this.$text.hide();
  }

  // ComboBox AutoComplete focus...
  $t.autocomplete.prototype.focus = function () {
    this.$text.focus();
  }
}

// ******** For Uploader, Custom Function ****************** //
var telerik_uploader_Loaded = false;
if (typeof ($t) != 'undefined')
  telerik_uploader_Loaded = (typeof ($t.upload) != 'undefined');

if (telerik_uploader_Loaded) {
  $t.upload.prototype.rebindAllCustomEvents = function () {
    var wrapper = this.wrapper;

    $t.bind(this.wrapper, {
      load: this.onLoad,
      select: this.onSelect,
      upload: this.onUpload,
      success: this.onSuccess,
      error: this.onError,
      complete: this.onComplete,
      cancel: this.onCancel,
      remove: this.onRemove
    });
  }
}


// 1- Framework Attribute bilgilerine göre element bulunur.
window.document.getElementByAttributeName = function (attrName, attrValue)
{
  return $("[" + attrName + "='" + attrValue + "']").get(0);
}

PageMethods.t$ = {};
document.elementData = {};

// 2- element içerisinde tetiklenecek "Enable", "Disable", "SetValue" gibi 
//    fonksiyonların çağrılması için Elemente ait Method dinamik olarak çalıştırılır.
PageMethods.t$.runElementMethod = function (attrName, attrValue, script)
{
  // element elde ediliyor.
  var elementtmp = document.getElementByAttributeName(attrName, attrValue);

  if ((elementtmp != null) && (typeof (elementtmp) != 'undefined') && (elementtmp.nodeName != '#text')) {
    // third Party Component Class Name  => tTextBox, tComboBox
    var elementClassName = elementtmp.getAttribute("TPCCN");

    var workingScript = "";
    if ((elementClassName != null) && (elementClassName != 'undefined') && (elementClassName != '')) 
    {
      // Telerik'teki Data kısmındaki nesne elde ediliyor. 
      document.elementData = $('#' + elementtmp.id).data(elementClassName);
      workingScript = "document.elementData." + script;
    }
    else // AutoComplete bileşeninde "$(element).data" kısmı yok. 
    {
      workingScript = "elementtmp." + script;
    }

    return eval(workingScript);
  }

  return "";
}

// 3- For Run Editor Method...
PageMethods.t$.runEditorMethod = function (attrValue, Script) 
{
  return PageMethods.t$.runElementMethod("EFI", attrValue, Script);
}

// 4- For Run Label Method...
PageMethods.t$.runLabelMethod = function (attrValue, Script)
{
  return PageMethods.t$.runElementMethod("ELFI", attrValue, Script);
}

// 5- Get Editor Value...
PageMethods.t$.getEditorValue = function (attrValue)
{
  return PageMethods.t$.runEditorMethod(attrValue, 'value()');
}

// 6- Set Editor Value...
PageMethods.t$.setEditorValue = function (attrValue, newValue)
{
  PageMethods.t$.runEditorMethod(attrValue, "value('" + newValue + "')");
}

// 7- Set Editor Enabled...
PageMethods.t$.enableEditor = function (attrValue) 
{
  PageMethods.t$.runEditorMethod(attrValue, 'enable()');
}

// 8- Set Editor Disabled...
PageMethods.t$.disableEditor = function (attrValue)
{
  PageMethods.t$.runEditorMethod(attrValue, 'disable()');
}

// 9- Set Editor Focus...
PageMethods.t$.focusEditor = function (attrValue) 
{
  PageMethods.t$.runEditorMethod(attrValue, 'focus()');
}

// 10- Set Editor Show...
PageMethods.t$.showEditor = function (attrValue)
{
  PageMethods.t$.runEditorMethod(attrValue, 'show()');
}

// 11- Set Editor Hide...
PageMethods.t$.hideEditor = function (attrValue) {
  PageMethods.t$.runEditorMethod(attrValue, 'hide()');
}

// 12- For Run Menu Method...
PageMethods.t$.runMenuMethod = function (attrValue, Script) 
{
  return PageMethods.t$.runElementMethod("MMAV", attrValue, Script);
}

PageMethods.t$.tmpMenu = {};

// onclick Function'ların tutulduğu array...
PageMethods.t$.arrFunc = [];

// 13- Telerik Menu Enabled...
// menuName aslında elementin ID değeri...
PageMethods.t$.enableMenu = function (attrValue, menuName, urlName) {
  PageMethods.t$.tmpMenu = $('#' + menuName).get(0);

  if (typeof (PageMethods.t$.tmpMenu) != 'undefined') {
    $('#' + PageMethods.t$.tmpMenu.id + ' img').attr('src', urlName);

    // for onclick enabled...
    if (PageMethods.t$.arrFunc[menuName])
      $(PageMethods.t$.tmpMenu).get(0).onclick = PageMethods.t$.arrFunc[menuName];

    PageMethods.t$.runMenuMethod(attrValue, 'enable(PageMethods.t$.tmpMenu)');

    $('#' + PageMethods.t$.tmpMenu.id + ' span').live('mouseenter', $t.hover).live('mouseleave', $t.leave);
  }
}

// 14- Telerik Menu Disabled...
// menuName aslında elementin ID değeri...
PageMethods.t$.disableMenu = function (attrValue, menuName, urlName) 
{
  PageMethods.t$.tmpMenu = $('#' + menuName).get(0);

  if (typeof (PageMethods.t$.tmpMenu) != 'undefined')
  {
    $('#' + PageMethods.t$.tmpMenu.id + ' img').attr('src', urlName);

    // for onclick disabled...
    if ($(PageMethods.t$.tmpMenu).get(0).onclick != null)
    {
      PageMethods.t$.arrFunc[menuName] = $(PageMethods.t$.tmpMenu).get(0).onclick;
      $(PageMethods.t$.tmpMenu).get(0).onclick = null;
    }

    PageMethods.t$.runMenuMethod(attrValue, 'disable(PageMethods.t$.tmpMenu)');

    // Bug vardı, düzeltildi...
    $('#' + PageMethods.t$.tmpMenu.id + ' span').live('mouseenter', $t.leave).live('mouseleave', $t.leave);
  }
}

// 15- hide Telerik Menu...
PageMethods.t$.hideMenu = function (attrValue, menuName) 
{
  var elementtmp = window.document.getElementByAttributeName("MMAV", attrValue);
  $(elementtmp).find('"#' + menuName + '"').hide();
}

// 16- show Telerik Menu...
PageMethods.t$.showMenu = function (attrValue, menuName)
{
  var elementtmp = window.document.getElementByAttributeName("MMAV", attrValue);
  $(elementtmp).find('"#' + menuName + '"').show();
}


//////////// HGOLabel Functions /////////////////
// 17- Get Label Caption...
PageMethods.t$.getLabelCaption = function (attrValue) {
  return PageMethods.t$.runLabelMethod(attrValue, 'text()');
}

// 18- Set Label Caption...
PageMethods.t$.setLabelCaption = function (attrValue, newValue) {
  PageMethods.t$.runLabelMethod(attrValue, "text('" + newValue + "')");
}

// 19- Set Label Enabled...
PageMethods.t$.enableLabel = function (attrValue) {
  PageMethods.t$.runLabelMethod(attrValue, 'enable()');
}

// 20- Set Label Disabled...
PageMethods.t$.disableLabel = function (attrValue) {
  PageMethods.t$.runLabelMethod(attrValue, 'disable()');
}

// 21- Label Show...
PageMethods.t$.showLabel = function (attrValue) {
  PageMethods.t$.runLabelMethod(attrValue, 'show()');
}

// 22- Label Hide...
PageMethods.t$.hideLabel = function (attrValue) {
  PageMethods.t$.runLabelMethod(attrValue, 'hide()');
}
//////////// HGOLabel Functions /////////////////


//////////// HGOWindow Functions /////////////////

// 23- For Run Window Method...
PageMethods.t$.runWindowMethod = function (attrValue, Script) {
  return PageMethods.t$.runElementMethod("TPCCN", attrValue, Script);
}

// 24- Get Window Caption...
PageMethods.t$.getWindowCaption = function (attrValue) {
  return PageMethods.t$.runWindowMethod(attrValue, 'title()');
}

// 25- Set Window Caption...
PageMethods.t$.setWindowCaption = function (attrValue, newValue) {
  PageMethods.t$.runWindowMethod(attrValue, "title('" + newValue + "')");
}

//////////// HGOWindow Functions /////////////////